package com.bancomer.batch.converters;

import java.util.List;
import java.util.Properties;

import com.bancomer.batch.Constants;
import com.bancomer.batch.MapConstants;
import com.bancomer.batch.pojo.Alert;
import com.bancomer.batch.pojo.Detail;
import com.bancomer.batch.pojo.FileHelper;
import com.bancomer.batch.pojo.Summary;
import com.bancomer.batch.util.StringConverter;
import com.blackbear.flatworm.FileCreator;

public class ObjectsToAuditFormatConverter{
	public static void convertToAuditSummary(List<Summary> summaryObjects,Alert alert) throws Exception{
		MapConstants properties = MapConstants.getInstance(alert.getProperties().getProperty("constantsFile"));
		final Properties alertConstants = properties.getConstants();
		FileHelper output = null;
		for(FileHelper file:alert.getOutputFiles()){
			if(file.getType().equals(alertConstants.getProperty("FILE_OUTPUT"))){
				if(file.getProperties().getProperty("outputType").equals(alertConstants.getProperty("OUTPUT_TYPE_SUMMARY"))){
					output=file;
					break;
				}
			}
		}
		output.setName(StringConverter.convertPatternToDate(output.getName()));
		FileCreator fileCreator = new FileCreator(output.getProperties().getProperty("xmlOutputConfig"),output.getLocalPath()+output.getName());
        fileCreator.open();
        fileCreator.setRecordSeperator(alertConstants.getProperty("END_OF_LINE"));
        for (Summary summary : summaryObjects) {
            fileCreator.setBean("resumen", summary);
            fileCreator.write("TSALAR");
        }
        fileCreator.close();
	}
	public static void convertToAuditDetail(List<Detail> detailObjects,Alert alert) throws Exception{
		MapConstants properties = MapConstants.getInstance(alert.getProperties().getProperty("constantsFile"));
		final Properties alertConstants = properties.getConstants();
		FileHelper output=null;
		for(FileHelper file:alert.getOutputFiles()){
			if(file.getType().equals(alertConstants.getProperty("FILE_OUTPUT"))){
				if(file.getProperties().getProperty("outputType").equals(alertConstants.getProperty("OUTPUT_TYPE_DETAIL"))){
					output = file;
					break;
				}
			}
		}
		output.setName(StringConverter.convertPatternToDate(output.getName()));
		FileCreator fileCreator = new FileCreator(output.getProperties().getProperty("xmlOutputConfig"),output.getLocalPath()+output.getName());
        fileCreator.open();
        fileCreator.setRecordSeperator(alertConstants.getProperty("END_OF_LINE"));
        for (Detail desglose : detailObjects) {
            fileCreator.setBean("desglose", desglose);
            fileCreator.write("TSALAD");
        }
        fileCreator.close();
	}
}
